import 'package:json_annotation/json_annotation.dart';
import 'package:flutter/material.dart';

part 'vault_item_model.g.dart';

/// 私密信息项模型
@JsonSerializable()
class VaultItemModel {
  /// 项目ID
  final String id;

  /// 项目标题（网站名称、应用名称等）
  final String title;

  /// 项目类型
  final VaultItemType type;

  /// 用户名/账号
  final String username;

  /// 密码
  final String password;

  /// 网站URL（可选）
  final String? url;

  /// 备注信息（可选）
  final String? notes;

  /// 图标（可选，可以是URL或资源名称）
  final String? icon;

  /// 创建时间
  @JsonKey(name: 'created_at')
  final DateTime createdAt;

  /// 更新时间
  @JsonKey(name: 'updated_at')
  final DateTime updatedAt;

  /// 是否收藏
  @JsonKey(name: 'is_favorite')
  final bool isFavorite;

  /// 构造函数
  VaultItemModel({
    required this.id,
    required this.title,
    required this.type,
    required this.username,
    required this.password,
    this.url,
    this.notes,
    this.icon,
    required this.createdAt,
    required this.updatedAt,
    this.isFavorite = false,
  });

  /// 从JSON创建对象
  factory VaultItemModel.fromJson(Map<String, dynamic> json) =>
      _$VaultItemModelFromJson(json);

  /// 转换为JSON
  Map<String, dynamic> toJson() => _$VaultItemModelToJson(this);

  /// 创建新的私密信息项
  factory VaultItemModel.create({
    required String title,
    required VaultItemType type,
    required String username,
    required String password,
    String? url,
    String? notes,
    String? icon,
    bool isFavorite = false,
  }) {
    final now = DateTime.now();
    return VaultItemModel(
      id: now.millisecondsSinceEpoch.toString(),
      title: title,
      type: type,
      username: username,
      password: password,
      url: url,
      notes: notes,
      icon: icon,
      createdAt: now,
      updatedAt: now,
      isFavorite: isFavorite,
    );
  }

  /// 复制并更新属性
  VaultItemModel copyWith({
    String? title,
    VaultItemType? type,
    String? username,
    String? password,
    String? url,
    String? notes,
    String? icon,
    bool? isFavorite,
  }) {
    return VaultItemModel(
      id: this.id,
      title: title ?? this.title,
      type: type ?? this.type,
      username: username ?? this.username,
      password: password ?? this.password,
      url: url ?? this.url,
      notes: notes ?? this.notes,
      icon: icon ?? this.icon,
      createdAt: this.createdAt,
      updatedAt: DateTime.now(),
      isFavorite: isFavorite ?? this.isFavorite,
    );
  }
}

/// 私密信息项类型
enum VaultItemType {
  /// 网站账号
  @JsonValue('website')
  website,

  /// 应用账号
  @JsonValue('app')
  app,

  /// 银行卡
  @JsonValue('bank_card')
  bankCard,

  /// 证件
  @JsonValue('id_card')
  idCard,

  /// 其他
  @JsonValue('other')
  other,
}

/// 私密信息项类型扩展
extension VaultItemTypeExtension on VaultItemType {
  /// 获取类型名称
  String get name {
    switch (this) {
      case VaultItemType.website:
        return '网站账号';
      case VaultItemType.app:
        return '应用账号';
      case VaultItemType.bankCard:
        return '银行卡';
      case VaultItemType.idCard:
        return '证件';
      case VaultItemType.other:
        return '其他';
    }
  }

  /// 获取类型图标
  IconData get icon {
    switch (this) {
      case VaultItemType.website:
        return Icons.language;
      case VaultItemType.app:
        return Icons.apps;
      case VaultItemType.bankCard:
        return Icons.credit_card;
      case VaultItemType.idCard:
        return Icons.badge;
      case VaultItemType.other:
        return Icons.more_horiz;
    }
  }
}

/// 常用网站图标
class WebsiteIcons {
  // 私有构造函数
  WebsiteIcons._();

  /// 根据URL获取网站图标
  static String? getIconByUrl(String? url) {
    if (url == null || url.isEmpty) return null;

    try {
      final uri = Uri.parse(url);
      final host = uri.host.toLowerCase();

      if (host.contains('google.com')) return 'google';
      if (host.contains('facebook.com')) return 'facebook';
      if (host.contains('twitter.com') || host.contains('x.com'))
        return 'twitter';
      if (host.contains('instagram.com')) return 'instagram';
      if (host.contains('linkedin.com')) return 'linkedin';
      if (host.contains('github.com')) return 'github';
      if (host.contains('amazon.com')) return 'amazon';
      if (host.contains('apple.com')) return 'apple';
      if (host.contains('microsoft.com')) return 'microsoft';
      if (host.contains('netflix.com')) return 'netflix';
      if (host.contains('spotify.com')) return 'spotify';
      if (host.contains('dropbox.com')) return 'dropbox';
      if (host.contains('slack.com')) return 'slack';
      if (host.contains('youtube.com')) return 'youtube';
      if (host.contains('pinterest.com')) return 'pinterest';
      if (host.contains('reddit.com')) return 'reddit';
      if (host.contains('twitch.tv')) return 'twitch';
      if (host.contains('discord.com')) return 'discord';
      if (host.contains('zoom.us')) return 'zoom';
      if (host.contains('alibaba.com') ||
          host.contains('taobao.com') ||
          host.contains('tmall.com')) return 'alibaba';
      if (host.contains('baidu.com')) return 'baidu';
      if (host.contains('tencent.com') || host.contains('qq.com'))
        return 'tencent';
      if (host.contains('weibo.com')) return 'weibo';
      if (host.contains('jd.com')) return 'jd';

      // 返回域名首字母作为默认图标
      return host.split('.')[0].substring(0, 1).toUpperCase();
    } catch (e) {
      return null;
    }
  }
}
